const router = require('koa-router')()
const JWT = require('jsonwebtoken');
const userList = [{
  "id": "1",
  "username": "admin",
  "password": "admin",
  "avatar": "https://robohash.org/123?set=set5",
  "nickname": "管理员",
  "role": "admin"
}, {
  "id": "2",
  "username": "user",
  "password": "user",
  "avatar": "https://robohash.org/123?set=set4",
  "nickname": "普通用户",
  "role": "user"
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body;
  const user = userList.find(v => v.username === username && v.password === password)
  ctx.body = user ?
    {
      code: 200,
      data: {
        userInfo: user,
        token: JWT.sign(user, '666666')
      },
      message: '登录成功， 欢迎' + user.nickname
    }
    :
    {
      code: 400,
      message: '账号密码错误， 请重新输入'
    }
})

module.exports = router
